package com.zhijian.warehouse.manage.service.impl;

import com.zhijian.util.BeanUtil;
import com.zhijian.warehouse.manage.dao.StoreAllotRecordProductMapper;
import com.zhijian.warehouse.manage.entity.po.StoreAllotRecordProduct;
import com.zhijian.warehouse.manage.entity.vo.request.StoreAllotRecordProductSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreAllotRecordProductSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreAllotRecordProductVo;
import com.zhijian.warehouse.manage.service.StoreAllotRecordProductService;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 调拨产品明细表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-11-03 15:30:56
 */
@Slf4j
@Service
public class StoreAllotRecordProductServiceImpl implements StoreAllotRecordProductService {

	@Autowired
    private StoreAllotRecordProductMapper mapper;

    @Override
    public StoreAllotRecordProduct selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreAllotRecordProductVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreAllotRecordProduct storeAllotRecordProduct) {
        Date now = new Date();
        storeAllotRecordProduct.setCreateTime(now);
        storeAllotRecordProduct.setUpdateTime(now);
        return mapper.insert(storeAllotRecordProduct) > 0 ? storeAllotRecordProduct.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreAllotRecordProduct storeAllotRecordProduct) {
        Date now = new Date();
        storeAllotRecordProduct.setCreateTime(now);
        storeAllotRecordProduct.setUpdateTime(now);
        return mapper.insertSelective(storeAllotRecordProduct) > 0 ? storeAllotRecordProduct.getId() : 0L;
    }

    @Override
    public boolean update(StoreAllotRecordProduct storeAllotRecordProduct) {
        Date now = new Date();
        storeAllotRecordProduct.setUpdateTime(now);
        return mapper.updateByPrimaryKey(storeAllotRecordProduct) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreAllotRecordProduct record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreAllotRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreAllotRecordProduct storeAllotRecordProduct = new StoreAllotRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeAllotRecordProduct);
        return this.insertSelective(storeAllotRecordProduct) != 0;
    }

    @Override
    public boolean modify(StoreAllotRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreAllotRecordProduct old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreAllotRecordProduct storeAllotRecordProduct = new StoreAllotRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeAllotRecordProduct);
        return this.update(storeAllotRecordProduct);
    }

    @Override
    public StoreAllotRecordProductVo selectOne(StoreAllotRecordProductSearchVo storeAllotRecordProductSearchVo) {
        return mapper.selectOne(storeAllotRecordProductSearchVo);
     }

    @Override
    public Integer count(StoreAllotRecordProductSearchVo storeAllotRecordProductSearchVo) {
        return mapper.count(storeAllotRecordProductSearchVo);
    }

    @Override
    public List<StoreAllotRecordProductVo> selectList(StoreAllotRecordProductSearchVo storeAllotRecordProductSearchVo) {
        return mapper.selectList(storeAllotRecordProductSearchVo);
    }

    @Override
    public Page<StoreAllotRecordProductVo> selectPage(StoreAllotRecordProductSearchVo storeAllotRecordProductSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeAllotRecordProductSearchVo.getPageNum()) || Objects.isNull(storeAllotRecordProductSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeAllotRecordProductSearchVo);
        //分页
        Page<StoreAllotRecordProductVo> page = new Page<>(true, count, storeAllotRecordProductSearchVo.getPageNum(), storeAllotRecordProductSearchVo.getPageSize());
        storeAllotRecordProductSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreAllotRecordProductVo> responseVos = this.selectList(storeAllotRecordProductSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreAllotRecordProduct> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StoreAllotRecordProductSearchVo storeAllotRecordProductSearchVo) {
        if (BeanUtil.isEmpty(storeAllotRecordProductSearchVo.getStoreAllotRecordId()))
            return;
        List<StoreAllotRecordProductVo> storeAllotRecordProductVos = this.selectList(storeAllotRecordProductSearchVo);
        if (BeanUtil.isEmpty(storeAllotRecordProductVos))
            return;
        List<Long> collect = storeAllotRecordProductVos.stream().map(StoreAllotRecordProductVo::getId).collect(Collectors.toList());
        this.deleteByIdList(collect);
    }
}
